package com.yuanchu.store.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanchu.common.exception.YuanChuException;
import com.yuanchu.common.utils.PageResult;
import com.yuanchu.common.utils.PageUtils;
import com.yuanchu.common.utils.Query;
import com.yuanchu.store.dao.YoshopUserGradeDao;
import com.yuanchu.store.dto.EditGradeParamsDto;
import com.yuanchu.store.dto.UserGradeDto;
import com.yuanchu.store.entity.YoshopUserGradeEntity;
import com.yuanchu.store.service.YoshopUserGradeService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.yuanchu.common.utils.DateFormatUtils.formatDatetime;


@Service("yoshopUserGradeService")
public class YoshopUserGradeServiceImpl extends ServiceImpl<YoshopUserGradeDao, YoshopUserGradeEntity> implements YoshopUserGradeService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<YoshopUserGradeEntity> page = this.page(
                new Query<YoshopUserGradeEntity>().getPage(params),
                new QueryWrapper<YoshopUserGradeEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<UserGradeDto> queryGradelist(Integer status) {
        if (status == null){
            status = 1;
        }
        List<YoshopUserGradeEntity> userGradeEntities = this.list(new QueryWrapper<YoshopUserGradeEntity>().eq("status", status));
        if (userGradeEntities == null) {
            return null;
        }
        return userGradeEntities.stream().map(YoshopUserGradeServiceImpl::getUserGrade).collect(Collectors.toList());
    }

    @Override
    public UserGradeDto getUserGradeInfo(Integer gradeId) {
        YoshopUserGradeEntity userGrade = this.getById(gradeId);
        return getUserGrade(userGrade);
    }

    @Override
    public PageResult<UserGradeDto> queryUserGradeList() {
        List<YoshopUserGradeEntity> gradeEntityList = this.list();
        List<UserGradeDto> collect = gradeEntityList.stream().map(grade -> {
            UserGradeDto userGrade = getUserGrade(grade);
            userGrade.setCreateTime(formatDatetime(grade.getCreateTime()));
            return userGrade;
        }).collect(Collectors.toList());
        return PageResult.formatPageResult(collect, 1, gradeEntityList.size(), gradeEntityList.size());
    }

    @Override
    public void editUserGrade(EditGradeParamsDto param) {
        Integer gradeId = param.getGradeId();
        YoshopUserGradeEntity grade = this.getById(gradeId);
        if (grade == null){
            return;
        }
        UserGradeDto form = param.getForm();
        grade.setEquity(JSON.toJSONString(form.getEquity()));
        grade.setUpgrade(JSON.toJSONString(form.getUpgrade()));
        BeanUtils.copyProperties(form, grade);
        boolean update = this.updateById(grade);
        if (!update){
            throw new YuanChuException("更新等级失败");
        }
    }

    @Override
    public void deleteUserGrade(Integer gradeId) {
        boolean update = this.update(new UpdateWrapper<YoshopUserGradeEntity>().eq("grade_id", gradeId).set("is_delete", 1));
        if (!update){
            throw new YuanChuException("删除等级失败");
        }
    }

    @NotNull
    private static UserGradeDto getUserGrade(YoshopUserGradeEntity userGrade) {
        UserGradeDto userGradeDto = new UserGradeDto();
        BeanUtils.copyProperties(userGrade, userGradeDto);
        // 将JSON字符串转换为Map对象
        String equityStr = userGrade.getEquity();
        Map<String, Object> mapEquity = JSON.parseObject(equityStr, Map.class);
        userGradeDto.setEquity(mapEquity);
        String upgradeStr = userGrade.getUpgrade();
        Map<String, Object> mapUpgrade = JSON.parseObject(upgradeStr, Map.class);
        userGradeDto.setUpgrade(mapUpgrade);
        userGradeDto.setCreateTime(formatDatetime(userGrade.getCreateTime()));
        userGradeDto.setUpdateTime(formatDatetime(userGrade.getUpdateTime()));
        return userGradeDto;
    }


}